package com.inhgroup.flashcards;

import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.inhgroup.flashcards.ui.FlashCardPanel;

/**
 * An application that attempts to replicate flash cards for the user.
 * 
 * The original intent of this is for practicing Japanese-English translations.
 * 
 * @author Branden Ogata
 *
 */

@SuppressWarnings("serial")
public class FlashCards extends JApplet
{
  /**
   * The logger for the FlashCards class.
   * 
   */
  private static Logger logger = LoggerFactory.getLogger(FlashCards.class.getName());  
  
  /**
   * The frame containing the Flash Cards program.
   * 
   */
  private JFrame frame;
  
  /**
   * The FlashCardPanel containing the display for the Flash Cards program.
   * 
   */
  private FlashCardPanel panel;
  
  /**
   * The menu bar.
   * 
   */
  private JMenuBar menuBar;
  
  /**
   * The File menu.
   * 
   */
  private JMenu fileMenu;
  
  /**
   * The Open menu item.
   * 
   */
  private JMenuItem openMenuItem;
  
  /**
   * The Exit menu item.
   * 
   */
  private JMenuItem exitMenuItem;
  
  /**
   * The Help menu.
   * 
   */
  private JMenu helpMenu;
  
  /**
   * The Help menu item.
   * 
   */
  private JMenuItem helpMenuItem;
  
  /**
   * The About menu item.
   * 
   */
  private JMenuItem aboutMenuItem;

  /**
   * Creates a new FlashCards window.
   * 
   */
  
  public FlashCards()
  {
    this.start();
  }
  
  /**
   * Starts the FlashCards program as an application.
   * 
   * @param args The String[] containing command line arguments; not currently used.
   *             
   */
  
  public static void main(String[] args)
  {
    new FlashCards();
  }
  
  /**
   * Initializes the FlashCards program as an applet.
   * 
   */
  
  @Override
  public final void start()
  {
    // Sets the look and feel to match the system of the user
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    // If any exception occurs, ignore for now; not sure how to test any of these cases
    catch (UnsupportedLookAndFeelException e)
    {
      logger.error(e.getMessage());
    }
    catch (ClassNotFoundException e)
    {
      logger.error(e.getMessage());
    }
    catch (InstantiationException e)
    {
      logger.error(e.getMessage());
    }
    catch (IllegalAccessException e)
    {
      logger.error(e.getMessage());
    }    
    
    this.frame = new JFrame("Flash Cards");
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Sets up the menus
    MenuListener listener = new MenuListener();
    Font menuFont = new Font(Font.SERIF, Font.PLAIN, 11);
    this.menuBar = new JMenuBar();
    this.fileMenu = new JMenu("File");
    this.fileMenu.setFont(menuFont);
    this.openMenuItem = new JMenuItem("Open");
    this.openMenuItem.setFont(menuFont);
    this.openMenuItem.addActionListener(listener);
    this.exitMenuItem = new JMenuItem("Exit");
    this.exitMenuItem.setFont(menuFont);
    this.exitMenuItem.addActionListener(listener);
    // Will not add Open until functionality is fully tested
/*
    this.fileMenu.add(this.openMenuItem);
 */
    this.fileMenu.add(this.exitMenuItem);
    this.menuBar.add(this.fileMenu);
    this.helpMenu = new JMenu("Help");
    this.helpMenu.setFont(menuFont);
    this.helpMenuItem = new JMenuItem("Help");
    this.helpMenuItem.setFont(menuFont);
    this.helpMenuItem.addActionListener(listener);
    this.aboutMenuItem = new JMenuItem("About");
    this.aboutMenuItem.setFont(menuFont);
    this.aboutMenuItem.addActionListener(listener);
    this.helpMenu.add(this.helpMenuItem);
    this.helpMenu.add(this.aboutMenuItem);
    this.menuBar.add(this.helpMenu);
    this.frame.setJMenuBar(this.menuBar);
    
    this.panel = new FlashCardPanel();
    this.frame.setContentPane(this.panel);
    
    this.frame.pack();
    this.frame.setExtendedState(this.frame.getExtendedState() | Frame.MAXIMIZED_BOTH);
    this.frame.setVisible(true);
    
    // Update font size
    this.panel.setFontSizes(this.frame.getSize().getWidth());
  }
  
  /**
   * Listens for any actions on the menus.
   * 
   */
  
  private class MenuListener implements ActionListener
  {
    /**
     * Handles any clicks on the menu that occur.
     * 
     * @param event The ActionEvent that recorded the menu action.
     * 
     */
    
    @Override
    public void actionPerformed(ActionEvent event)
    {
      if (event.getSource() == FlashCards.this.openMenuItem)
      {
        logger.error("Should not reach this yet");
      }
      else if (event.getSource() == FlashCards.this.exitMenuItem)
      {
        FlashCards.this.frame.dispose();
      }
      else if (event.getSource() == FlashCards.this.helpMenuItem)
      {
        JLabel helpLabel = new JLabel();
        helpLabel.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        String helpText = "<html>";
        helpText += "Help not implemented yet;<br />";
        helpText += "this version of inh-group-flash-cards<br />";
        helpText += "is intended for testers to evaluate the<br />";
        helpText += "intuitiveness and usability of this<br />";
        helpText += "application and thus does not include<br />";
        helpText += "instructions.";
        helpText += "</html>";
        helpLabel.setText(helpText);
        
        JOptionPane.showMessageDialog(FlashCards.this.frame, helpLabel, "Help",
                                      JOptionPane.INFORMATION_MESSAGE);
      }
      else if (event.getSource() == FlashCards.this.aboutMenuItem)
      {
        JLabel aboutLabel = new JLabel();
        aboutLabel.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        String aboutText = "<html>";
        aboutText += "Written by Branden Ogata<br />";
        aboutText += "Copyright 2013 The INH Group";
        aboutText += "</html>";
        aboutLabel.setText(aboutText);
        
        JOptionPane.showMessageDialog(FlashCards.this.frame, aboutLabel, "About",
                                      JOptionPane.INFORMATION_MESSAGE);        
      }
    }
    
  }
}